/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.mailfolder;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.mailfolder.Mailbox;

/**
 * @author Administrator (email:xxh@163.com)
 * @since 2013-02-21
 */
@Service("mailboxService")
public class MailboxServiceImpl implements MailboxService {

    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of Mailbox
     ****************************************/
    public void insert(Mailbox mailbox) throws Exception {
        mailbox.setBoxid(TemplateUtil.genUUID());
        iDataSet4iBatis.insert(mailbox);
    }

    /****************************************
     * Update a instace of Mailbox
     ****************************************/
    public void update(Mailbox mailbox) throws Exception {
        iDataSet4iBatis.update(mailbox);
    }

    /****************************************
     * Delete a instace of Mailbox
     ****************************************/
    public void delete(Mailbox mailbox) throws Exception {
        iDataSet4iBatis.delete(mailbox);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(Mailbox mailbox) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    public void batchUpdate(Mailbox mailbox) throws Exception {
        String userId = mailbox.getUserid();
        String statmentId = mailbox.getStatmentId();
        String folderType = mailbox.getFolderid();

        String mailId = mailbox.getId();
        String[] ids = mailId.split(",");

        List<BaseModel> mailboxList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Mailbox mb = new Mailbox();
            mb.setStatmentId(statmentId);
            mb.setMailid(id);
            mb.setUserid(userId);
            if ("4".equals(folderType)) {
                // 如果在垃圾箱中, 直接设置删除标记(delflag)为1
                mb.setDelflag(1);
            } else {
                // 其他类型, 放在垃圾箱中
                mb.setFolderid("4");
            }
            mailboxList.add(mb);
        }
        iDataSet4iBatis.batchUpdate(mailboxList);

    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(Mailbox mailbox) throws Exception {
        String statementId = mailbox.getStatmentId();
        String mainId = mailbox.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> mailboxList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Mailbox model = new Mailbox();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            mailboxList.add(model);
        }
        iDataSet4iBatis.batchDelete(mailboxList);
    }

    /****************************************
     * Query instaces of Mailbox
     ****************************************/
    public List<BaseModel> queryAsList(Mailbox mailbox) throws Exception {
        String queryListStatement = "";
        if (mailbox.isPaging()) {
            queryListStatement = mailbox.getStatmentId();
            mailbox.setStatmentId("Mailbox.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(mailbox);
            mailbox.setTotalCount(baseModel.getTotalCount());
            mailbox.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(mailbox);
    }

    /****************************************
     * Query a instaces of Mailbox
     ****************************************/
    public BaseModel queryAsObject(Mailbox mailbox) throws Exception {
        return iDataSet4iBatis.queryAsObject(mailbox);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
